Ext.define('SKJF.controller.MSTCURCUKAI',{
	extend: 'Ext.app.Controller',
	views: ['MASTER.v_mstcurcukai','MASTER.v_mstcurcukai_form'],
	models: ['m_mstcurcukai'],
	stores: ['s_mstcurcukai'],
	
	requires: ['Ext.ModelManager'],
	
	refs: [{
		ref: 'Listmstcurcukai',
		selector: 'Listmstcurcukai'
	}, {
		ref: 'v_mstcurcukai_form',
		selector: 'v_mstcurcukai_form'
	}, {
		ref: 'Savemstcurcukai',
		selector: 'v_mstcurcukai_form #save'
	}, {
		ref: 'Createmstcurcukai',
		selector: 'v_mstcurcukai_form #create'
	}, {
		ref: 'MSTCURCUKAI',
		selector: 'MSTCURCUKAI'
	}],


	init: function(){
		this.control({
			'MSTCURCUKAI': {
				'afterrender': this.mstcurcukaiAfterRender
			},
			'v_mstcurcukai_form': {
				'afterlayout': this.mstcurcukaiAfterLayout
			},
			'Listmstcurcukai': {
				'selectionchange': this.enableDelete,
				'itemdblclick': this.updateListmstcurcukai
			},
			'Listmstcurcukai button[action=create]': {
				click: this.createRecord
			},
			'Listmstcurcukai button[action=delete]': {
				click: this.deleteRecord
			},
			'Listmstcurcukai button[action=print]': {
				click: this.printRecords
			},
			'v_mstcurcukai_form button[action=save]': {
				click: this.saveV_mstcurcukai_form
			},
			'v_mstcurcukai_form button[action=create]': {
				click: this.saveV_mstcurcukai_form
			},
			'v_mstcurcukai_form button[action=cancel]': {
				click: this.cancelV_mstcurcukai_form
			}
		});
	},
	
	mstcurcukaiAfterRender: function(){
		var mstcurcukaiStore = this.getListmstcurcukai().getStore();
		mstcurcukaiStore.load();
	},
	
	mstcurcukaiAfterLayout: function(){this.getV_mstcurcukai_form().down('#PERIODE_MULAI_field').focus(false, true);this.getV_mstcurcukai_form().down('#PERIODE_SAMPAI_field').focus(false, true);this.getV_mstcurcukai_form().down('#MATA_UANG_field').focus(false, true);
	},
	
	createRecord: function(){
		var getListmstcurcukai	= this.getListmstcurcukai();
		var getV_mstcurcukai_form= this.getV_mstcurcukai_form(),
			form			= getV_mstcurcukai_form.getForm();
		var getSavemstcurcukai	= this.getSavemstcurcukai();
		var getCreatemstcurcukai	= this.getCreatemstcurcukai();
		
		/* grid-panel */
		getListmstcurcukai.setDisabled(true);
        
		/* form-panel */
		form.reset();
		getV_mstcurcukai_form.down('#PERIODE_MULAI_field').setReadOnly(false);getV_mstcurcukai_form.down('#PERIODE_SAMPAI_field').setReadOnly(false);getV_mstcurcukai_form.down('#MATA_UANG_field').setReadOnly(false);
		getSavemstcurcukai.setDisabled(true);
		getCreatemstcurcukai.setDisabled(false);
		getV_mstcurcukai_form.setDisabled(false);
		
		this.getMSTCURCUKAI().setActiveTab(getV_mstcurcukai_form);		
	},
	
	enableDelete: function(dataview, selections){
		this.getListmstcurcukai().down('#btndelete').setDisabled(!selections.length);
	},
	
	updateListmstcurcukai: function(me, record, item, index, e){
		var getMSTCURCUKAI		= this.getMSTCURCUKAI();
		var getListmstcurcukai	= this.getListmstcurcukai();
		var getV_mstcurcukai_form= this.getV_mstcurcukai_form(),
			form			= getV_mstcurcukai_form.getForm();
		var getSavemstcurcukai	= this.getSavemstcurcukai();
		var getCreatemstcurcukai	= this.getCreatemstcurcukai();
		
		getSavemstcurcukai.setDisabled(false);
		getCreatemstcurcukai.setDisabled(true);
		getV_mstcurcukai_form.down('#PERIODE_MULAI_field').setReadOnly(true);getV_mstcurcukai_form.down('#PERIODE_SAMPAI_field').setReadOnly(true);getV_mstcurcukai_form.down('#MATA_UANG_field').setReadOnly(true);		
		getV_mstcurcukai_form.loadRecord(record);
		
		getListmstcurcukai.setDisabled(true);
		getV_mstcurcukai_form.setDisabled(false);
		getMSTCURCUKAI.setActiveTab(getV_mstcurcukai_form);
	},
	
	deleteRecord: function(dataview, selections){
		var getstore = this.getListmstcurcukai().getStore();
		var selection = this.getListmstcurcukai().getSelectionModel().getSelection()[0];
		if(selection){
			Ext.Msg.confirm('Confirmation', 'Are you sure to delete this data: "PERIODE_MULAI" = "'+selection.data.PERIODE_MULAI+'","PERIODE_SAMPAI" = "'+selection.data.PERIODE_SAMPAI+'","MATA_UANG" = "'+selection.data.MATA_UANG+'","ID" = "'+selection.data.ID+'"?', function(btn){
				if (btn == 'yes'){
					getstore.remove(selection);
					getstore.sync();
				}
			});
			
		}
	},
	
	printRecords: function(){
		var getstore = this.getListmstcurcukai().getStore();
		var jsonData = Ext.encode(Ext.pluck(getstore.data.items, 'data'));
		
		Ext.Ajax.request({
			method: 'POST',
			url: 'c_mstcurcukai/printRecords',
			params: {data: jsonData},
			success: function(response){
				var result=eval(response.responseText);
				switch(result){
				case 1:
					win = window.open('./temp/mstcurcukai.html','mstcurcukai_list','height=400,width=900,resizable=1,scrollbars=1, menubar=1');
					break;
				default:
					Ext.MessageBox.show({
						title: 'Warning',
						msg: 'Unable to print the grid!',
						buttons: Ext.MessageBox.OK,
						animEl: 'save',
						icon: Ext.MessageBox.WARNING
					});
					break;
				}  
			}
		});
	},
	
	saveV_mstcurcukai_form: function(){
		var getMSTCURCUKAI		= this.getMSTCURCUKAI();
		var getListmstcurcukai 	= this.getListmstcurcukai();
		var getV_mstcurcukai_form= this.getV_mstcurcukai_form(),
			form			= getV_mstcurcukai_form.getForm(),
			values			= getV_mstcurcukai_form.getValues();
		var store 			= this.getStore('s_mstcurcukai');
		
		if (form.isValid()) {
			var jsonData = Ext.encode(values);
			
			Ext.Ajax.request({
				method: 'POST',
				url: 'c_mstcurcukai/save',
				params: {data: jsonData},
				success: function(response){
					store.reload({
						callback: function(){
							var newRecordIndex = store.findBy(
								function(record, id) {
									if (record.get('PERIODE_MULAI') === values.PERIODE_MULAI && record.get('PERIODE_SAMPAI') === values.PERIODE_SAMPAI && record.get('MATA_UANG') === values.MATA_UANG) {
										return true;
									}
									return false;
								}
							);
							/* getListmstcurcukai.getView().select(recordIndex); */
							getListmstcurcukai.getSelectionModel().select(newRecordIndex);
						}
					});
					
					getV_mstcurcukai_form.setDisabled(true);
					getListmstcurcukai.setDisabled(false);
					getMSTCURCUKAI.setActiveTab(getListmstcurcukai);
				}
			});
		}
	},
	
	createV_mstcurcukai_form: function(){
		var getMSTCURCUKAI		= this.getMSTCURCUKAI();
		var getListmstcurcukai 	= this.getListmstcurcukai();
		var getV_mstcurcukai_form= this.getV_mstcurcukai_form(),
			form			= getV_mstcurcukai_form.getForm(),
			values			= getV_mstcurcukai_form.getValues();
		var store 			= this.getStore('s_mstcurcukai');
		
		if (form.isValid()) {
			var jsonData = Ext.encode(values);
			
			Ext.Ajax.request({
				method: 'POST',
				url: 'c_mstcurcukai/save',
				params: {data: jsonData},
				success: function(response){
					store.reload();
					
					getV_mstcurcukai_form.setDisabled(true);
					getListmstcurcukai.setDisabled(false);
					getMSTCURCUKAI.setActiveTab(getListmstcurcukai);
				}
			});
		}
	},
	
	cancelV_mstcurcukai_form: function(){
		var getMSTCURCUKAI		= this.getMSTCURCUKAI();
		var getListmstcurcukai	= this.getListmstcurcukai();
		var getV_mstcurcukai_form= this.getV_mstcurcukai_form(),
			form			= getV_mstcurcukai_form.getForm();
			
		form.reset();
		getV_mstcurcukai_form.setDisabled(true);
		getListmstcurcukai.setDisabled(false);
		getMSTCURCUKAI.setActiveTab(getListmstcurcukai);
	}
	
});